package com.bothin.experiment4.service.impl;


import com.bothin.experiment4.dto.ProductFormDTO;
import com.bothin.experiment4.enums.ProductFormStatusEnum;
import com.bothin.experiment4.enums.ProductFormTypeEnum;
import com.bothin.experiment4.model.ProductForm;
import com.bothin.experiment4.service.IProductFormService;
import com.bothin.experiment4.service.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by bothin
 * At 2019/12/18 0:46
 * Description:
 */
@Service
public class ProductFormServiceImpl extends BaseServiceImpl<ProductForm> implements IProductFormService {
    @Override
    public ProductForm findById(Object id) {
        return productFormMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ProductForm> findAll() {
        return productFormMapper.selectAll();
    }

    @Override
    @Transactional
    public void deleteById(Object id) {
        productFormMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(ProductForm product) {
        productFormMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    @Transactional
    public void insert(ProductForm product) {
        productFormMapper.insertSelective(product);
    }

    @Override
    public List<ProductForm> findAllOutForm() {
        return productFormMapper.findAllByType(ProductFormTypeEnum.OUT_FORM.getCode());
    }

    @Override
    public List<ProductForm> findAllInForm() {
        return productFormMapper.findAllByType(ProductFormTypeEnum.IN_FORM.getCode());
    }

    @Override
    public List<ProductFormDTO> findAllOutFormDTO() {
        return productFormMapper.findAllByTypeDTO(ProductFormTypeEnum.OUT_FORM.getCode());
    }

    @Override
    public List<ProductFormDTO> findAllInFormDTO() {
        return productFormMapper.findAllByTypeDTO(ProductFormTypeEnum.IN_FORM.getCode());
    }

    @Override
    public List<ProductFormDTO> findAllOutUnhandleDTO() {
        return productFormMapper.findAllByTypeAndUnhandleDTO(ProductFormTypeEnum.OUT_FORM.getCode());
    }

    @Override
    public List<ProductFormDTO> findAllOutHandledDTO() {
        return productFormMapper.findAllByTypeAndHandledDTO(ProductFormTypeEnum.OUT_FORM.getCode());
    }

    @Override
    public List<ProductFormDTO> findAllInUnhandleDTO() {
        return productFormMapper.findAllByTypeAndUnhandleDTO(ProductFormTypeEnum.IN_FORM.getCode());
    }

    @Override
    public List<ProductFormDTO> findAllInHandledDTO() {
        return productFormMapper.findAllByTypeAndHandledDTO(ProductFormTypeEnum.IN_FORM.getCode());
    }

    @Override
    public List<ProductForm> findAllNeedInForm() {
        return productFormMapper.findAllProductQuantityLessThanZero(ProductFormTypeEnum.IN_FORM.getCode());
    }

    @Override
    public int changeFinshStatus(String id, Integer status) {
        ProductForm productForm = new ProductForm();
        productForm.setFormId(id);
        productForm.setFinshStatus(status);
        return productFormMapper.updateByPrimaryKeySelective(productForm);
    }

    /**
     * 判断这个合同下的所有发货单是否完成
     * @param id
     * @return
     */
    @Override
    public boolean judgeAllFinshStatusByContactId(String id) {
        List<Integer> status = productFormMapper.findAllFinshStatusByContactId(id);
        for (Integer flag : status) {
            if (flag!= ProductFormStatusEnum.FINISH.getCode()){
                return false;
            }
        }
        return true;
    }

    @Override
    public List<ProductFormDTO> findByContactIdDTO(String contactId) {
        return productFormMapper.findByContactIdDTO(contactId);
    }

    @Override
    public List<ProductFormDTO> findAllByTypeAndKeywordDTO(Integer type, String key) {
        return productFormMapper.findAllByTypeAndKeywordDTO(type,"%"+key+"%");
    }

    @Override
    public ProductFormDTO findOneFormDTO(String id) {
        return productFormMapper.findOneByIdDTO(id);
    }

}
